package com.os.service;

import com.os.dao.PersonRepository;
import com.os.entity.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.NoRollbackRuleAttribute;

import javax.transaction.Transactional;

@Service
public class DemoServiceImpl implements DemoService{

    @Autowired
    private PersonRepository personRepository;

    @Transactional(rollbackOn={IllegalArgumentException.class})  //指定特定异常时候，数据回滚
    @Override
    public Person savePersonWithRollBack(Person person) {
        Person person1 = personRepository.save(person);

        if(person.getName().equals("Matt")){
            throw new IllegalArgumentException("Matt已存在，数据将回滚！");
        }
        return person1;
    }

    @Transactional(dontRollbackOn={IllegalArgumentException.class})  //指定特定异常的时候，数据不回滚
    @Override
    public Person savePersonWithoutRollBack(Person person) {
        Person person1 = personRepository.save(person);

        if(person.getName().equals("Matt")){
            throw new IllegalArgumentException("Matt虽已经存在，数据将不会回滚！");
        }
        return person1;
    }
}
